package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.marriage.CrossGroupMarriageMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.C_MARRIAGE_APPOINT_RESP, desc = "跨服指向联姻响应")
public class CrossMarriageAppointResponeCmd extends CrossCommand {
    private MarriageInfo marriageInfo;
    private BigInteger limitEarnSpeed = BigInteger.ZERO;
    private long appointUserId;
    private int result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossMarriageProto.CrossMarriageAppointRespMsg reqMsg = CrossMarriageProto.CrossMarriageAppointRespMsg.parseFrom(packet.getBytes());
        marriageInfo = MarriagePb.parseMarriageInfo(reqMsg.getApplyData());
        result = reqMsg.getRet();
        if(0 == result) {
            String value = reqMsg.getLimitEarnSpeed();
            if (!StringUtils.isNullOrEmpty(value)) {
                limitEarnSpeed = new BigInteger(value);
            }
            appointUserId = packet.getPlayerId();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long publishTime = System.currentTimeMillis() / 1000;
        marriageInfo.setLimitEarnSpeed(limitEarnSpeed);
        marriageInfo.setPublishTime(publishTime);
        ret = CrossGroupMarriageMgr.publishAppointMarriageResponse(result, marriageInfo, appointUserId, serverId);
        CrossMarriageProto.CrossMarriageApplyPublishCrossRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageApplyPublishCrossRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setChildUid(marriageInfo.getChildUid());
        if(0 == ret) {
            respMsg.setLimitEarnSpeed(limitEarnSpeed.toString());
            respMsg.setAppointUserId(appointUserId);
            respMsg.setPartnerServerId(serverId);
            respMsg.setPublishTime(publishTime);
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_MARRIAGE_PUBLISH_RESP, respMsg);
        MessageHelper.sendPacket(marriageInfo.getServerId(), marriageInfo.getUserId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
